# coding: UTF-8

import config
import random
import threading

import base_func
import topo_data

tunnel_info_set = []

def import_current_network() :
    topo_data.set_resource_location(config.global_local_current_test_topo)

    # 初始化网络拓扑信息
    topo_input_params = {}
    topo_input_params['resource-location'] = config.global_remote_current_test_topo
    base_func.init_topology_data(topo_input_params)

    # 初始化网络隧道信息
    tunnel_input_params = {}
    tunnel_input_params['resource-location'] = config.global_remote_current_test_topo + 'standard/'
    base_func.init_tunnel_info_data(tunnel_input_params)
    base_func.init_tunnel_stat_data(tunnel_input_params)

    # 导入现网流量
    current_traffic = {}
    current_traffic['gold-flow'] = config.global_current_gold_flow
    current_traffic['silver-flow'] = config.global_current_silver_flow
    current_traffic['iron-flow'] = config.global_current_iron_flow
    current_traffic['copper-flow'] = config.global_current_copper_flow

    base_func.import_current_network(current_traffic)

def create_random_tunnel_info() :
    # 随机选定隧道源节点和目的节点
    node_count = topo_data.get_node_count()

    if node_count == 0 :
        print 'Empty Topology'
        return

    random_src_index = random.randint(1, node_count)
    random_dst_index = random.randint(1, node_count)

    while random_src_index == random_dst_index :
        random_dst_index = random.randint(1, node_count)

    src_ip_index = 255 - random_src_index
    dst_ip_index = 255 - random_dst_index

    src_ip = "192.168.1.%d" % (src_ip_index)
    dst_ip = "192.168.1.%d" % (dst_ip_index)

    prio = random.randint(1, 4)
    if prio == 1:
        priority = 'gold'
        bw = random.randint(1, config.global_current_gold_flow)
    elif prio == 2:
        priority = 'silver'
        bw = random.randint(1, config.global_current_silver_flow)
    elif prio == 3:
        priority = 'iron'
        bw = random.randint(1, config.global_current_iron_flow)
    elif prio == 4:
        priority = 'copper'
        bw = random.randint(1, config.global_current_copper_flow)

    tunnel_info = {}
    tunnel_info['name'] = '%d-%d-TT' % (src_ip_index, dst_ip_index)
    tunnel_info['priority'] = priority
    tunnel_info['src-ip'] = src_ip
    tunnel_info['dst-ip'] = dst_ip
    tunnel_info['bandwidth-demand'] = '%d' % (bw)
    tunnel_info['delay-demand'] = '4096'
    tunnel_info['hop-demand'] = '256'
    tunnel_info['pass-nodes'] = []
    tunnel_info['pass-links'] = []
    tunnel_info['avoid-nodes'] = []
    tunnel_info['avoid-links'] = []

    tunnel_id = base_func.create_tunnel_info(tunnel_info)
    tunnel_info_set.append((tunnel_id, tunnel_info))


def batch_new_tunnel(count) :
    for i in range(count) :
        create_random_tunnel_info()


def period_batch_tunnel_task() :
    batch_new_tunnel(config.global_optimization_count)
    t = threading.Timer(config.global_optimization_period, period_batch_tunnel_task)
    t.start()


def execute_period_optimization() :
    topo_data.set_resource_location(config.global_local_current_test_topo)

    # 初始化网络拓扑信息
    topo_input_params = {}
    topo_input_params['resource-location'] = config.global_remote_current_test_topo
    base_func.init_topology_data(topo_input_params)

    # 重新配置全局重优化周期
    config_info = {}
    config_info['optimization-period'] = config.global_optimization_period

    base_func.regulate_tno_config(config_info)

    # 启动周期性优化过程
    base_func.launch_periodic_global_optimization()

    # 周期性创建COUNT条隧道
    period_batch_tunnel_task()


def simulate_link_flash_process() :
    pass


def simulate_node_flash_process() :
    pass


def simulate_burst_traffic_rise() :
    pass


def simulate_burst_traffic_fall() :
    pass


def simulate_numerous_tunnel_create() :
    pass


def simulate_numerous_tunnel_remove() :
    pass